package com.st.controller.headend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.st.entity.Goods;
import com.st.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
//商城商品控制类
@Controller
@RequestMapping("sg")
public class ShopGoodsController {
    @Autowired
    private IGoodsService goodsService;
    @RequestMapping("query")
    public ModelAndView list(Goods goods,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        String userPhone= (String) session.getAttribute("userPhone");
        goods.setGoodsStatus(1);
        //判断页码变量是否为空，如果为空就赋值1 表示第一页
        if (goods.getPageNum() == null) {
            goods.setPageNum(1);
        }
        //一个查5条
        goods.setPageSize(6);
        //分页查询，注意该方法会对后面的第一个查询做分页
        PageHelper.startPage(goods.getPageNum(), goods.getPageSize());
        //1.查询商品列表（分页查询+条件查询）
        List<Goods> list = goodsService.query(goods);
        //把查出来的列表放到pageInfo中
        //分页信息，pageNum-页码 pageSize-单页记录数 ，pages-总页数
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("superiorId",goods.getSuperiorId());
        mv.addObject("userPhone",userPhone);
        mv.setViewName("/headend/goods_list");
        return mv;
    }
    @RequestMapping("detail")
    public ModelAndView detail(Goods goods, HttpServletRequest req){
        HttpSession session=req.getSession();
       String userPhone= (String) session.getAttribute("userPhone");
        List<Goods> list=goodsService.query(goods);
        Goods obj=list.get(0);
        ModelAndView mv=new ModelAndView();
        mv.addObject("obj",obj);
        mv.addObject("userPhone",userPhone);
        mv.setViewName("/headend/goods_detail");
        return mv;
    }
}
